<?php namespace Admin\Controller;
/*
 * 类别控制器
 * */
 	class CategoryController extends CommonController
 	{
 		public $db;
 		public function __init()
		{
			$this->db = new \Common\Model\Category;
		}
 		/*
		 * 分类列表
		 * */
		 public function index()
		 {
		 	$data = $this->db->orderBy("csort","asc")->get();
			$data = Data::tree($data,"cname");
			View::with("data",$data);
		 	View::make("cate");
		 }
		 /*
		  * 添加类别
		  * */
		  public function addCate()
		  {
		  	if(IS_AJAX){
				if(!$this->db->add_cate()){
					//p($this->db->getError());die;
					View::ajax(array("status"=>0,"message"=>$this->db->getError()));
				}
				View::ajax(array("status"=>1,"message"=>"添加成功"));
			}
		  	View::make();
			
		  }
		 /*
		  * 编辑分类
		  * */
		  public function resetCate()
			 {
			 	//获得get参数cid
			 	$cid = Q("get.cid",0,"intval");
			 	if(IS_AJAX){
			 		//修改数据
					//$cid = Q("get.cid",0,"intval");
					p($cid);die;
					//p($this->db->reset_cate());die;
				if(!$this->db->reset_cate($cid)){
					
					View::ajax(array("status"=>0,"message"=>$this->db->getError()));
				}
					View::ajax(array("status"=>1,"message"=>"编辑成功"));
				}
			 	//获得员数据
			 	//获得get参数cid
			 	//$cid = Q("get.cid",0,"intval");
				//p($cid);die;
				//获得cid对应的数据
				$data = $this->db->where("cid","=",$cid)->first();
				//p($data);die;
				View::with("data",$data);
				//获得分类  （要去掉自己和子类）
				$catedata = $this->db->getCataData($cid);
					//sp($cid);die;
				//p($catedata);
				View::with('catedata',$catedata);
				
				
			 	View::make();
			 }
		 /*
		  *添加子类
		  * */
		  public function addSon()
		  {
		  	//获得get参数
		  	$cid = Q("cid",0,"intval");
			//p($cid);
			$pcate = $this->db->where("cid",$cid)->first();
			//p($pcate);
			View::with("pcate",$pcate);
			//添加数据
			if(IS_AJAX){
				if(!$this->db->add_cate()){
					View::ajax(array("status"=>0,"message"=>$this->db->getError()));
				}
				View::ajax(array("status"=>1,"message"=>"添加成功"));
			}
		  	View::make();
		  }
		  /*
		   * 删除
		   * */
		   public function del()
		   {
		   		//获得cid
		   		$cid = Q("cid",0,"intval");
		   		if(!$this->db->del($cid)){
		   			$this->error("删除失败");
		   		}else{
		   			$this->success("删除成功","Category/index");
		   		}
		   }
 	}
